package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.DoctorLeftAdapter;
import com.xn.app.muying.adpter.DoctorRightAdapter;
import com.xn.app.muying.adpter.MineYiYuanAdapter;
import com.xn.app.muying.bean.DoctorGsonBean;
import com.xn.app.muying.bean.MineYiYuanBean;
import com.xn.app.muying.fragment.HomeFragment;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/31.
 */
public class DoctorActivity extends AppCompatActivity implements HttpUtil.OnGetDataListener, AdapterView.OnItemClickListener, View.OnClickListener {

    private ListView lv1;
    private ListView lv2;

    private DoctorLeftAdapter doctorLeftAdapter;
    private DoctorRightAdapter doctorRightAdapter;
    private LinearLayout ll;
    private TextView hosptial_tv;
    private MineYiYuanAdapter adapter;
    private Handler handler = new Handler();
    private ArrayList<MineYiYuanBean> list;
    private ArrayList<DoctorGsonBean> list_bean;
    private int id;
    private int mClickPosition;
    private int mLeftClickPosition;
    private ImageView back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);

        lv1 = (ListView) findViewById(R.id.activity_doctor_listview1);
        lv2 = (ListView) findViewById(R.id.activity_doctor_listview2);
        hosptial_tv = (TextView) findViewById(R.id.activity_doctor_tv);
        back = (ImageView) findViewById(R.id.activity_doctor_back);

        back.setOnClickListener(this);

        adapter = new MineYiYuanAdapter(this);
        HttpUtil.getData(APIMethod.Mine_JiuZhenYiYuan, handler, new HttpUtil.OnGetDataListener() {
            @Override
            public void onJsonData(String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray items = jsonObject.getJSONArray("items");
                    list = new ArrayList<>();
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject object = items.getJSONObject(i);
                        String name = object.optString("name");
                        id = object.optInt("id");
                        MineYiYuanBean bean = new MineYiYuanBean();
                        bean.setName(name);
                        bean.setId(id);
                        list.add(bean);

                    }
                    adapter.getData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        HttpUtil.getData(APIMethod.Home_Doctor_Left, handler, this);
        doctorLeftAdapter = new DoctorLeftAdapter(DoctorActivity.this, handler);
        doctorRightAdapter = new DoctorRightAdapter(DoctorActivity.this, handler);
        lv1.setAdapter(doctorLeftAdapter);
        lv2.setAdapter(doctorRightAdapter);

        lv1.setOnItemClickListener(this);
        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("doctorlist_id", list_bean.get(mClickPosition).getSubCompanies().get(mLeftClickPosition).getSubCompanies().get(i).getId()+"");
                Log.i("doctorlist_id",list_bean.get(mClickPosition).getSubCompanies().get(mLeftClickPosition).getSubCompanies().get(i).getId()+"");
                intent.setClass(DoctorActivity.this, DoctorListActivity.class);
                startActivity(intent);
            }
        });
        hosptial_tv.setOnClickListener(this);
    }

    @Override
    public void onJsonData(String data) {
        list_bean = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(data);
            JSONArray items = object.getJSONArray("items");
            for (int i = 0; i < items.length(); i++) {
                list_bean.add(new Gson().fromJson(items.get(i).toString(), DoctorGsonBean.class));
            }

            for (int i = 0; i < list_bean.size(); i++) {
                if (list_bean.get(i).getId() == id) {
                    hosptial_tv.setText(list.get(i).getName().toString());
                    doctorLeftAdapter.getData(list_bean.get(i).getSubCompanies());
                    if (list_bean.get(i).getSubCompanies() != null && list_bean.get(i).getSubCompanies().size() > 1) {
                        mClickPosition = i;
                        mLeftClickPosition =0;
                        doctorRightAdapter.getData((ArrayList<DoctorGsonBean.SubCompaniesBean>) list_bean.get(i).getSubCompanies().get(0).getSubCompanies());
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        doctorRightAdapter.getData((ArrayList<DoctorGsonBean.SubCompaniesBean>) list_bean.get(mClickPosition).getSubCompanies().get(i).getSubCompanies());
        mLeftClickPosition = i;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_doctor_tv:
                AlertDialog.Builder radioDialog = new AlertDialog.Builder(this);
                radioDialog.setTitle("医院");
                radioDialog.setIcon(R.mipmap.ic_launcher);
                radioDialog.setSingleChoiceItems(adapter, 0,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doctorRightAdapter.getListData().clear();
                                doctorRightAdapter.notifyDataSetChanged();
                                hosptial_tv.setText(list.get(which).getName().toString());
                                for (int j = 0; j < list_bean.size(); j++) {
                                    if (list_bean.get(j).getId() == list.get(which).getId()) {
                                        mClickPosition = j;
                                        doctorLeftAdapter.getData(list_bean.get(j).getSubCompanies());
                                        if (list_bean.get(j).getSubCompanies() != null && list_bean.get(j).getSubCompanies().size() > 1) {
                                            doctorRightAdapter.getData((ArrayList<DoctorGsonBean.SubCompaniesBean>) list_bean.get(j).getSubCompanies().get(0).getSubCompanies());
                                        }
                                    }
                                }
                            }
                        });
                radioDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialogRadio = radioDialog.create();
                dialogRadio.show();
                break;

            case R.id.activity_doctor_back:
                finish();
                break;
        }
    }


}
